﻿/*
 * Created by SharpDevelop.
 * Date: 06.11.2011 г.
 * Time: 23:28 ч.
 * 
 */
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace CaesarCoder
{
	/// <summary>
	/// Description of Manager.
	/// </summary>
	public partial class Manager : Form
	{
		Config config;
		string ConfigDir = "";
		int dgvSelectedRow = 0;
		
		public Manager(string ConfigDir, int MainFormSelectedIndex)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			this.dgvSelectedRow = MainFormSelectedIndex;
			this.ConfigDir = ConfigDir;
		}
		
		void manager_loaded(object sender, EventArgs e)
		{
			this.config = new Config(this.ConfigDir);
			this.dgvTables.DataSource = this.config.ConversionTables;
			
			this.dgvTables.ClearSelection();
			this.dgvTables.Rows[this.dgvSelectedRow].Selected = true;
			this.RefreshButtons();
			this.dgvTables.UserDeletingRow += new DataGridViewRowCancelEventHandler(user_deleting_row);
		}

		void user_deleting_row(object sender, DataGridViewRowCancelEventArgs e)
		{
			if (MessageBox.Show("Are you sure you want to delete the table?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
			{
				// delete ConversionTable physically from disk
				((ConversionTable)(e.Row.DataBoundItem)).DeleteFromDisk();
				this.RefreshButtons();
			}
			else
			{
				// cancel the UserDeletingRow event
				e.Cancel = true;
			}
		}
		
		void RefreshButtons()
		{
			if (this.config != null && this.config.ConversionTables.Count > 0)
			{
				this.btnEdit.Enabled = true;
				this.btnDelete.Enabled = true;
			}
			else 
			{
				this.btnEdit.Enabled = true;
				this.btnDelete.Enabled = true;
			}
		}
		
		void create_new_table(object sender, EventArgs e)
		{
			Editor edit = new Editor(null, this.ConfigDir);
			if (edit.ShowDialog() == DialogResult.OK)
			{
				this.config.Reload();
				this.dgvTables.DataSource = this.config.ConversionTables;
				// invalidate the DataGridView so that it repaints
				this.dgvTables.Invalidate();
			}
		}
		
		void edit_table(object sender, EventArgs e)
		{
			Editor edit = new Editor((ConversionTable)this.dgvTables.SelectedRows[0].DataBoundItem, this.ConfigDir);
			if (edit.ShowDialog() == DialogResult.OK)
			{
				// reload ConversionTable
				((ConversionTable)this.dgvTables.SelectedRows[0].DataBoundItem).Reload();
				// invalidate the modified row so that it repaints
				this.dgvTables.InvalidateRow(this.dgvTables.SelectedRows[0].Index);
			}
		}
		
		void delete_table(object sender, EventArgs e)
		{
			if (MessageBox.Show("Are you sure you want to delete the table?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
			{
				((ConversionTable)(this.dgvTables.SelectedRows[0].DataBoundItem)).DeleteFromDisk();
				this.dgvTables.Rows.Remove(this.dgvTables.SelectedRows[0]); //MultiSelect=false so we have only 1 row selected at a time
				this.RefreshButtons();
			}
		}
	}
}
